﻿using UnityEngine;
using System.Collections;

using KaoticSSB.Players;
using KaoticSSB.Globals;

namespace KaoticSSB.Weapons
{
	public class ThrowingWeapon : WeaponBase 
	{
		public Vector3 PreviousAngle
		{
			get;
			private set;
		}
		public Vector3 PreviousVelocity
		{
			get;
			private set;
		}

		// Use this for initialization
		public override void Start () 
		{
			base.Start();
		}

		/*
		 * \brief Update is called once per frame
		 */
		public override void Update () 
		{
			base.Update();

			if(!mCollided)
			{
				PreviousAngle = transform.rotation.eulerAngles;
				PreviousVelocity = this.rigidbody2D.velocity;
			}

			Rigidbody2D rigidBody = this.gameObject.rigidbody2D;
			if(rigidBody != null)
			{
				Vector2 velocity = rigidbody2D.velocity;
				this.gameObject.transform.LookAt(velocity);
				if(velocity.x < -0.1f)
				{
					Vector3 newEuler = new Vector3(transform.eulerAngles.x + 180.0f, transform.eulerAngles.y, transform.eulerAngles.z);
					transform.eulerAngles = newEuler;
				}
			}

			//kill
			if(transform.position.y < Global.KILL_Y)
			{
				Destroy (this.gameObject);

				PlayerManager playerManager = GameObject.FindObjectOfType<PlayerManager>();
				Player player = playerManager.GetPlayer(PlayerID);
				if(player != null)
				{
					player.PickupWeapon();
				}
			}
		}

		public override void OnCollisionEnter2D(Collision2D collisionObject)
		{
			base.OnCollisionEnter2D(collisionObject);
			if(collisionObject.gameObject.tag == "Environment")
			{
				Active = false;
			}
		}
	}
}